package com.woniu.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.client.ElasticClient;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.dto.ResponseCodeDto;
import com.woniu.car.dto.TServiceDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.entity.TService;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.param.ElasticServerAddParameter;
import com.woniu.car.param.ElasticServerUpdateParameter;
import com.woniu.car.param.ServiceParam;
import com.woniu.car.param.UpdateServiceParam;
import com.woniu.car.service.TServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ServiceController
 * @Description TODO
 * @Author 李磊
 * @Version 1.0
 **/
@RestController
@RequestMapping("/service")
@Api(tags = "服务产品模块")
@Slf4j
public class ServiceController {

    @Resource
    private TServiceService tServiceService;
    @Resource
    private ElasticClient elasticClient;

    /**
    *@Author 李磊
    *@Description 服务产品新增
    *@Param [serviceParam]
    *@return com.woniu.CarPlatform.common.ResponseEntity
    **/
    @PostMapping("/insert")
    @ApiOperation("新增服务")
    public ResponseEntity insert(@Validated ServiceParam serviceParam){


        if (null == serviceParam.getServiceName()) {
            throw new ParamException(CommonConstant.INSERTNULL);
        }


        TService service = new TService();
        BeanUtils.copyProperties(serviceParam,service);

        TService insert = tServiceService.insert(service);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                insert);
    }


    /**
    *@Author 李磊
    *@Description 查询所有服务或者查询单个服务详情
    *@Param [id]
    *@return com.woniu.CarPlatform.common.ResponseEntity
    **/
    @GetMapping("/query")
    @ApiOperation("查询服务（商家精准或者所有）")
    public ResponseEntity queryAllService(Integer id) throws Exception{

        if (null == id) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        List<TService> list = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("status",CommonConstant.ZERO);

        if(id == 0) {
            list = tServiceService.list(wrapper);
        }else {
            wrapper.eq("shop_id",id);
            list = tServiceService.list(wrapper);
            if (null == list) {
                throw new ServiceSException(CommonConstant.RESULTNULL);
            }
        }

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                list);
    }

    /**
    *@Author 李磊
    *@Description 修改服务商品
    *@Param [updateServiceParam]
    *@return com.woniu.CarPlatform.common.ResponseEntity
    **/
    @PutMapping("/update")
    @ApiOperation("修改服务商品")
    public ResponseEntity updateService(@Validated UpdateServiceParam updateServiceParam) {

        if (null == updateServiceParam) {
            throw new ParamException(CommonConstant.UPDATENULL);
        }

        TService byId = tServiceService.getById(updateServiceParam.getServiceId());

        if (null == byId) {
            throw new ParamException(CommonConstant.UPDATEIDNULL);
        }


        Date date = new Date();
        TService service = new TService();

        BeanUtils.copyProperties(updateServiceParam,service);
        service.setRawUpdateTime(date);

        tServiceService.updateById(service);
        TService byId1 = tServiceService.getById(service.getServiceId());
        TServiceDto tServiceDto = new TServiceDto();
        BeanUtils.copyProperties(byId1,tServiceDto);

        ElasticServerUpdateParameter elastic = new ElasticServerUpdateParameter();
        elastic.setShopId(byId1.getShopId());
        elastic.setServiceName(byId1.getServiceName());
        elastic.setDescription(byId1.getDescription());
        elastic.setReceiverId(byId1.getServiceId());

        elasticClient.updateService(elastic);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tServiceDto);
    }

    /**
    *@Author 李磊
    *@Description 删除服务商品
    *@Param [serviceId]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/delete")
    @ApiOperation("删除服务商品")
    public ResponseEntity delete(Integer serviceId) throws Exception{

        if (null == serviceId || serviceId <= 0) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }
        TService service = new TService();
        service.setServiceId(serviceId);
        service.setStatus(CommonConstant.THREE);
        Date date = new Date();
        service.setRawUpdateTime(date);

        boolean b = tServiceService.updateById(service);

        if (!b) {
            throw new ServiceSException(CommonConstant.DELWRONG);
        }

        TService byId = tServiceService.getById(serviceId);
        TServiceDto tServiceDto = new TServiceDto();
        BeanUtils.copyProperties(byId,tServiceDto);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tServiceDto);
    }

    /**
    *@Author 李磊
    *@Description 服务商品上架
    *@Param [serviceId]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/serviceUp")
    @ApiOperation("服务商品上架")
    public ResponseEntity upStatus(Integer serviceId) throws Exception{

        if (null == serviceId || serviceId <= 0) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TService tService = tServiceService.getById(serviceId);

        if (null == tService) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        if (!tService.getStatus().equals(CommonConstant.ZERO)) {
            throw new ParamException(CommonConstant.UPWRONG);
        }

        TService service = new TService();
        service.setServiceId(serviceId);
        service.setStatus(CommonConstant.ONE);
        Date date = new Date();
        service.setRawUpdateTime(date);

        boolean b = tServiceService.updateById(service);

        if (!b) {
            throw new ServiceSException(CommonConstant.DELWRONG);
        }

        TService byId = tServiceService.getById(serviceId);
        TServiceDto tServiceDto = new TServiceDto();
        BeanUtils.copyProperties(byId,tServiceDto);

        ElasticServerAddParameter elastic = new ElasticServerAddParameter();
        elastic.setId(byId.getServiceId()+"");
        elastic.setServiceName(byId.getServiceName());
        elastic.setShopId(byId.getShopId());
        elastic.setDescription(byId.getDescription());

        elasticClient.upService(elastic);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tServiceDto);
    }

    /**
    *@Author 李磊
    *@Description 服务商品下架
    *@Param [serviceId]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/serviceDown")
    @ApiOperation("服务商品下架")
    public ResponseEntity downStatus(Integer serviceId) throws Exception{

        if (null == serviceId || serviceId <= 0) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TService tService = tServiceService.getById(serviceId);

        if (null == tService) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        if (!tService.getStatus().equals(CommonConstant.ONE)) {
            throw new ParamException(CommonConstant.DOWNWRONG);
        }

        TService service = new TService();
        service.setServiceId(serviceId);
        service.setStatus(CommonConstant.ZERO);
        Date date = new Date();
        service.setRawUpdateTime(date);

        boolean b = tServiceService.updateById(service);

        if (!b) {
            throw new ServiceSException(CommonConstant.DELWRONG);
        }

        TService byId = tServiceService.getById(serviceId);
        TServiceDto tServiceDto = new TServiceDto();
        BeanUtils.copyProperties(byId,tServiceDto);

        elasticClient.downService(byId.getServiceId());

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tServiceDto);
    }


}
